function validatePostCode(postcode){
	var validPostCode = checkPostCode(postcode);
	return validPostCode;
}

function clearArray(array){
	while(array.length > 0){
		array.pop();
	}
}

function validateField(id, validationTypes){
	var field = $("#"+id);
	var error = false;
	var message = "";
	var inputLabel = getLabelOfInput(id);
	var label = inputLabel.text().replace(":", "");
	var testRegex = function(regexp, test){
		return regexp.test(test);
	}
	var isEmpty = function(field){
		return field.val().trim() === "";
	}
	for(var i = 0; i<validationTypes.length; i++){
		switch(validationTypes[i]){
			case "empty":
				if(field.val().trim() === ""){
					error = true;
					message += "\n-The field "+label+" cannot be empty";
				}
				break;
			case "number":
				debugger;
				var regexp = /^[+]?([0-9]+(?:[\.][0-9]*)?|\.([0-9]{2}))$/;
				//var regexp = /^\d+$/;
				if(!isEmpty(field) && !testRegex(regexp,field[0].value)){
					error = true;
					message += "\n-The field "+label+" must be a valid number"
				}
				break;
			case "email":
				var regexp = /.{1,}.@.{1,}/;
				if(!isEmpty(field) && !testRegex(regexp,field.val())){
					error = true;
					message += "\n-The field "+label+" must contain a valid email";
				}
				break;
			case "textonly":
				var regexp = /^[a-zA-Z ]+$/;
				if(!isEmpty(field) && !testRegex(regexp,field.val())){
					error = true;
					message += "\n-The field "+label+" has invalid characters";
				}
				break;
			case "postcode":
				if(!validatePostCode(field.val())){
					error = true;
					message += "\n-The field "+label+" must contain a valid postcode";
				}
				break;
			case "dateOfBirth":
			debugger;
				if(!isEmpty(field) && !validateDateOfBirth(field.val())){
					error = true;
					message += "\n-You must be 18 or older.";
				}
				break;
			case "telephone":

				break;
		}
	}
	return message;
}


function getLabelOfInput(id){
	return $("label[for="+id+"]");
}

function parseDate(dateString){
	return Date.parse(dateString);
}

function validateDateOfBirth(dateString){
	var date = new Date(parseDate(dateString));
	var today = new Date();
	var difYear = today.getFullYear() - date.getFullYear();
	if(difYear >= 18){
		return true;
	}
	return false;
}

function concatArray(str, array){
	if(!str){
		str = "";
	}
	for(var i = 0; i<array.length; i++){
		str += array[i];
	}
	return str;
}

function getCurrentURL(){
	return document.URL;
}

function getURL(page){
	var urlArray = getCurrentURL().split("/");
	urlArray[urlArray.length - 1] = page;
	var url = urlArray.join("/");
	return url;
}

function configureLoginLayer(r)
{
	$("#loginLayerWrapper").empty();
	$("#loginLayerWrapper").append(r.htmlString);
	showLoginLayer();
	$(".loginLayerContentsBox").css("zIndex", "101");
	$("#loginLayer").on("click", function(){
		hideLoginLayer();
	});
	$("#loginLayer > div > div").on("click", function(e){e.stopPropagation()});
}

function configureListingAndProfileLayer(r)
{
	$("#profileAndListingContainer").empty();
	$("#profileAndListingContainer").append(r.htmlString);
}

function configureListingEditor(htmlString)
{
	$("#listingFormContainer").remove();
	$("#profileAndListingContainer").append(htmlString);
}

function showLoginLayer(){
	document.getElementById("loginLayer").style.opacity = "1";
	document.getElementById("loginLayer").style.zIndex = "100";
	document.getElementById("loginLayer").style.display = "table";
}

function hideLoginLayer(){
	$("#loginLayer").css("opacity", "0");
	$("#loginLayer").css("zIndex", "-1");
	$("#loginLayer").css("display", "none");
}

function dontSubmit(){
	debugger;
	return false;
}

function showLoadingLayer(){
	$("#loadingLayer").css("opacity", "1");
	$("#loadingLayer").css("zIndex", "999");
}

function hideLoadingLayer(){
	$("#loadingLayer").css("opacity", "0");
	$("#loadingLayer").css("zIndex", "-1");
}

function showHideElement(elements, show, showClass, hideClass)
{
	if(!showClass){
		showClass = "showClass";	
	}
	if(!hideClass){
		hideClass = "hideClass";
	}
	var classToRemove = hideClass;
	var classToAdd = showClass;

	if(!show)
	{
		classToRemove = showClass;
		classToAdd = hideClass;
	}
	for(var i = 0; i<elements.length; i++)
	{
		var el = elements[i];
		if(typeof el == "string"){
			el = $("#"+el);
		}
		el.removeClass(classToRemove);
		el.addClass(classToAdd);
	}	
}

function ImageValidator(minWidth, minHeight, maxSize)
{
	this.minWidth = minWidth ? minWidth : 260;
	this.minHeight = minHeight ? minHeight : 325;
	this.maxSize = maxSize ? maxSize : 3000; //Kb

	this.INVALID_WIDTH_MESSAGE = "-Invalid image dimensions: The width is too small (min width allowed: "+this.minWidth+").\n";
	this.INVALID_HEIGHT_MESSAGE  = "-Invalid image dimensions: The height is too small (min height allowed: "+this.minHeight+").\n";
	this.INVALID_SIZE_MESSAGE = "-The file size must be less than "+this.maxSize+".\n";
	var theValidator = this;
	this.validate = function(file, ifValidDo)
	{
		var reader = new FileReader();
	    var image  = new Image();

	    reader.readAsDataURL(file);  
	    reader.onload = function(_file) {
	        image.src    = _file.target.result;          
	        image.onload = function() {
	            var w = this.width,
	                h = this.height,
	                t = file.type,                          
	                n = file.name,
	                s = ~~(file.size/1024);
	            var error = false;
	            var errorMessage = "";
	            if(w < theValidator.minWidth)
	            {
	            	errorMessage += theValidator.INVALID_WIDTH_MESSAGE;
	            	error = true;
	            }

	            if(h < theValidator.minHeight)
	            {
	            	errorMessage += theValidator.INVALID_HEIGHT_MESSAGE;
	            	error = true;
	            }

	            if(s > theValidator.maxSize)
	            {
	            	errorMessage += theValidator.INVALID_SIZE_MESSAGE;
	            	error = true;
	            }
	            if(error)
	            {
	            	alert(errorMessage);
	            	return;
	            }
	            if(ifValidDo)
	            {
	            	ifValidDo();
	            }
	            
	        };
	        image.onerror= function() {
	            alert('Invalid file type: '+ file.type);
	        };      
	    };

	};
}

function addSuffixToFilepath(url, suffix)
{
	var urlParts = url.split(".");
	urlParts[urlParts.length - 2] = urlParts[urlParts.length - 2] + suffix;
	return urlParts.join(".");
}